﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class UserRoleDAO : IDAO<UserRole>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(UserRole entity)
        {
            agrDb.UserRoles.AddObject(entity);
        }

        public void remove(UserRole entity)
        {
            agrDb.UserRoles.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (UserRole userRole in agrDb.UserRoles.Where(s => s.UserRoleCode == code))
            {
                agrDb.UserRoles.DeleteObject(userRole);
            }
        }

        public void merge(UserRole entity)
        {
            foreach (UserRole userRole in agrDb.UserRoles.Where(s => s.UserRoleCode == entity.UserRoleCode))
            {
                userRole.UserRoleType = entity.UserRoleType;
            }

            //Select ALL
            //var userRoles = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public UserRole findById(string code)
        {
            UserRole userRole = new UserRole();

            foreach (UserRole role in agrDb.UserRoles.Where(s => s.UserRoleCode == code))
            {
                userRole = role;
            }

            return userRole;
        }

        public IEnumerable<UserRole> findAll()
        {
            var userRoles = (from role in agrDb.UserRoles
                            select role);

            return userRoles;
        }

        public IEnumerable<UserRole> findInRange(int firstResult, int maxResuslts)
        {
            var userRoles = (from role in agrDb.UserRoles
                            select role);

            return userRoles.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var userRoles = (from role in agrDb.UserRoles
                            select role).ToList();

            return userRoles.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}